package com.demo.controller;

import com.demo.dao.MajorMapper;
import com.demo.dao.ManagerMapper;
import com.demo.dao.StudentMapper;
import com.demo.dao.TeacherMapper;
import com.demo.entity.Manager;
import com.demo.entity.Student;
import com.demo.entity.Teacher;
import com.demo.util.ExcelUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by 67380 on 2016/10/26.
 */
@Controller
@RequestMapping("/")
@SessionAttributes({"user","role"})

public class CollegeController {
    @Resource
    ManagerMapper managerDao;

    @Resource
    StudentMapper studentDao;

    @Resource
    TeacherMapper teacherDao;

    @Resource
    MajorMapper majorDao;

    @RequestMapping("/student")
    public String student(Model model, @ModelAttribute("user")Manager manager){
        List<Student> studentList=studentDao.selectAllWithDesire(manager.getCollege());
        List<Teacher> teacherList=teacherDao.selectteaWithMajor(manager.getCollege());
        model.addAttribute("teacherList",teacherList);
        model.addAttribute("studentList",studentList);
        return "college/stumanager";
    }
    @RequestMapping("/de")
    public String de(String sid,String tid,int opt){
        Student student=new Student();
        student.setSid(sid);
        student.setSteacherid(tid);
        studentDao.updateByPrimaryKeySelective(student);
        if(opt==0)
            return "redirect:/student";
        else
            return "redirect:/student";
    }

    @RequestMapping("/exportStuExcel")
    public void exportStuExcel(HttpServletResponse response, @ModelAttribute("user")Manager manager){
        List<Student> studentList=studentDao.selectfinishWithDesire(manager.getCollege());
        String[] title={"准考证号","姓名","专业","身份证号","导师"};
        List<Teacher> teacherList=teacherDao.selectfinishWithDesire(manager.getCollege());
        String[] title1={"工号","姓名","专业","学院"};
        ExcelUtil.exportStuExcel("excel.xlsx",title,studentList,title1,teacherList,response);
    }

    @RequestMapping("/exportAllExcel")
    public void exportAllExcel( HttpServletResponse response){
        List<Student> studentList=studentDao.selectAllfinishWithDesire();
        String[] title={"准考证号","姓名","专业","身份证号","导师"};
        List<Teacher> teacherList=teacherDao.selectAllfinishWithDesire();
        String[] title1={"工号","姓名","专业","学院"};
        ExcelUtil.exportStuExcel("excel.xlsx",title,studentList,title1,teacherList,response);
    }

}
